package cn.lyq.card.ui.my

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStateAtLeast
import cn.lyq.card.BuildConfig
import cn.lyq.card.R
import cn.lyq.card.base.BaseFg
import cn.lyq.card.global.SPKeyGlobal
import cn.lyq.card.manager.UserManager
import cn.lyq.card.ui.aboutus.AboutUsActivity
import cn.lyq.card.ui.address.AddressManagerActivity
import cn.lyq.card.ui.advice.AdviceActivity
import cn.lyq.card.ui.bank.BankCardActivity
import cn.lyq.card.ui.customer.CustomerSLinkActivity
import cn.lyq.card.ui.message.MessageActivity
import cn.lyq.card.ui.realname.RealNameAuthActivity
import cn.lyq.card.ui.result.ResultActivity
import cn.lyq.card.ui.setting.SysSettingActivity
import cn.lyq.card.ui.userinfo.UserInfoActivity
import cn.lyq.card.ui.wallet.WalletActivity
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.toMoney
import com.blankj.utilcode.util.SPUtils
import kotlinx.android.synthetic.main.fg_me.*


class MyFg : BaseFg() {

    override fun getLayoutId(): Int = R.layout.fg_me

    override fun initListener(view: View) {
        super.initListener(view)
        bgUser.setClick {
            startActivity(UserInfoActivity::class.java)
        }
        tvCheckDetail.setClick {
            startActivity(WalletActivity::class.java)
        }
        bgBalance.setClick {
            startActivity(WalletActivity::class.java)
        }
        tvRealName.setClick {
            if (BuildConfig.DEBUG) {
                startActivity(RealNameAuthActivity::class.java)
                return@setClick
            }
            when (UserManager.instance.value?.idCardStatus ?: 3) {
                0 -> ResultActivity.start(requireContext(), 1)
                1 -> ResultActivity.start(requireContext(), 2)
                2 -> ResultActivity.start(requireContext(), 3)
                3 -> startActivity(RealNameAuthActivity::class.java)
            }
        }
        tvMessage.setClick {
            startActivity(MessageActivity::class.java)
        }
        tvAddress.setClick {
            startActivity(AddressManagerActivity::class.java)
        }
        tvBankCard.setClick {
            startActivity(BankCardActivity::class.java)
        }
        tvCustomerService.setClick {
            startActivity(CustomerSLinkActivity::class.java)
        }
        tvAdvice.setClick {
            startActivity(AdviceActivity::class.java)
        }
        tvAboutUs.setClick {
            startActivity(AboutUsActivity::class.java)
        }
        tvSetting.setClick {
            startActivity(SysSettingActivity::class.java)
        }
    }

    override fun initObserver() {
        super.initObserver()
        UserManager.instance.observe(this, Observer {
            it?.let {
                lifecycleScope.launchWhenResumed {
                    imgAvatar.loadUrl(it.userIcon)
                    tvUserName.text = if (it.agentName?.isEmpty() != false) {
                        it.trueName ?: "请设置昵称"
                    } else {
                        it.agentName ?: ""
                    }
                    tvInviteCode.text = "邀请编码：${it.invitationCode ?: ""}"
                    tvBalance.text = it.money?.toDoubleOrNull()?.toMoney() ?: "0"
                }
            }
        })
        UserManager.unreadNotice.observe(this, Observer {
            val src = if (it == 0) R.drawable.ic_message else R.drawable.ic_message_new
            tvMessage.setCompoundDrawablesWithIntrinsicBounds(0, src, 0, 0)
        })
        tvLevel.text = SPUtils.getInstance().getString(SPKeyGlobal.APP.LEVEL_NAME, "")
    }
}